package com.adcc.loadsheet.billing.util;

import com.adcc.aoc.devLib.common.log.Log;
import com.adcc.loadsheet.billing.configuration.SystemConfig;
import com.adcc.loadsheet.billing.service.LogService;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class LogUtil {

    @Autowired
    public LogService efbLog;

    private static LogUtil logUtil;

    @Autowired
    private SystemConfig systemConfig;

    //异常日志类型（与页面下拉选项保持一致）
    public static final int ERROR_LOG_TYPE = 30;

    //JOB日志类型（与页面下拉选项保持一致）
    public static final int JOB_LOG_TYPE = 29;

    //系统操作日志类型
//    public static final int SYSTEMOPERATION_LOG_TYPE = 2;

    @PostConstruct
    public void init() {
        logUtil = this;
        logUtil.efbLog = this.efbLog;
        logUtil.systemConfig = this.systemConfig;
    }

    //异常日志
    public static void errorLog(Class clazz, Exception e, String methodName) {
        Log.error(clazz, methodName, e);
        try {
            String s = ExceptionUtils.getStackTrace(e);
            logUtil.efbLog.errorLog(LogUtil.ERROR_LOG_TYPE, String.format("%s:%s —— %s —— %s", clazz.getSimpleName(), clazz.getName(), methodName, s));
        } catch (Exception ex) {
            Log.error(LogUtil.class, "errorLog has error:", ex);
        }
    }

    //操作日志
    public static void operationLog(Integer operationType, String content, String operator) {
        try {
            logUtil.efbLog.operationLog(operationType, content, operator);
        } catch (Exception ex) {
            Log.error(LogUtil.class, "operationLog has error:", ex);
        }
    }

    //job日志
    public static void jobLog(Class clazz,String methodName,String content) {
        try {
            logUtil.efbLog.jobLog(LogUtil.JOB_LOG_TYPE, String.format("%s:%s —— %s —— %s", clazz.getSimpleName(), clazz.getName(), methodName,content));
        } catch (Exception ex) {
            Log.error(LogUtil.class, "operationLog has error:", ex);
        }
    }
}
